home *** CD-ROM | disk | FTP | other *** search
/ SGI Developer Toolbox 6.1 / SGI Developer Toolbox 6.1 - Disc 4.iso / public / bit / src / ulib / bbox.c next >
C/C++ Source or Header  |  1994-08-01  |  2KB  |  68 lines

  1. /************************************************************************
  2.  * $Id: bbox.c,v 0.80 1994/02/24 09:48:11 zhao Exp $
  3.  *
  4.  *.  Copyright(c) 1993,1994 by T.C. Zhao
  5.  *   All rights reserved.
  6.  *.
  7.  *
  8.  * Find the bounding box of a rotated rectangle.
  9.  * The rectangle is centered at (x,y) and has a width w and a height h.
  10.  ***********************************************************************/
  11. #if !defined(lint) && defined(F_ID)
  12. char *id_bbox = "$Id: bbox.c,v 0.80 1994/02/24 09:48:11 zhao Exp $";
  13. #endif
  14.  
  15. #include "ulib.h"
  16. #include <math.h>
  17.  
  18. void
  19. find_bbox(int x, int y, int w, int h, float ia,
  20.       int *bx, int *by, int *bw, int *bh)
  21. {
  22.     double fx[4], fy[4], sa, ca;
  23.     double xi, xf, yi, yf;
  24.     double pi = acos(-1.0);
  25.     int i;
  26.  
  27.     if (Abs(ia) < 1.0e-5)
  28.       {
  29.       *bx = x - (w + 1) / 2;
  30.       *by = y - (h + 1) / 2;
  31.       *bw = w + 2;
  32.       *bh = h + 2;
  33.       return;
  34.       }
  35.  
  36.     sa = sin(ia * pi / 180.0);
  37.     ca = cos(ia * pi / 180.0);
  38.  
  39.     fx[0] = (-w * ca + h * sa) * 0.5 + x;
  40.     fy[0] = (-w * sa - h * ca) * 0.5 + y;
  41.  
  42.     fx[1] = (w * ca + h * sa) * 0.5 + x;
  43.     fy[1] = (w * sa - h * ca) * 0.5 + y;
  44.  
  45.     fx[2] = (w * ca - h * sa) * 0.5 + x;
  46.     fy[2] = (w * sa + h * ca) * 0.5 + y;
  47.  
  48.     fx[3] = (-w * ca - h * sa) * 0.5 + x;
  49.     fy[3] = (-w * sa + h * ca) * 0.5 + y;
  50.  
  51.     /* find the bbox */
  52.     xi = xf = fx[0];
  53.     yi = yf = fy[0];
  54.  
  55.     for (i = 1; i < 4; i++)
  56.       {
  57.       xi = Min(xi, fx[i]);
  58.       xf = Max(xf, fx[i]);
  59.       yi = Min(yi, fy[i]);
  60.       yf = Max(yf, fy[i]);
  61.       }
  62.  
  63.     *bx = (xi - 1.5);
  64.     *by = (yi - 1.5);
  65.     *bw = (xf - xi + 2.8);
  66.     *bh = (yf - yi + 2.8);
  67. }
  68.